package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import view.AdminWnd;
import view.TeacherWnd;

/**
 *
 * @author cska
 *
 */

public class deleteAdapter implements ActionListener {

    protected AdminWnd adminWnd;
    protected AdminCtrl adminCtrl;
    protected TeacherWnd teacherWnd;
    protected TeacherCtrl teacherCtrl;
    protected int deleteMode;

    public static final int DELETE_GROUP    =   1;
    public static final int DELETE_STUDENT  =   2;
    public static final int DELETE_TEACHER  =   3;
    public static final int DELETE_SUBJECT  =   4;
    public static final int DELETE_GTS      =   5;
    public static final int DELETE_ADMIN    =   6;
    public static final int DELETE_LESSON   =   7;
    public static final int DELETE_SKIP     =   8;
    public static final int DELETE_LAB      =   9;
    public static final int DELETE_LS       =  10;
    public static final int DELETE_INTERIM  =  11;

    public deleteAdapter(AdminWnd adminWnd, AdminCtrl adminCtrl, int deleteMode) {
        this.adminWnd = adminWnd;
        this.adminCtrl = adminCtrl;
        this.deleteMode = deleteMode;
    }

    public deleteAdapter(TeacherWnd teacherWnd, TeacherCtrl teacherCtrl, int deleteMode) {
        this.teacherWnd = teacherWnd;
        this.teacherCtrl = teacherCtrl;
        this.deleteMode = deleteMode;
    }

    public void actionPerformed(ActionEvent e) {
        switch (deleteMode) {
            case DELETE_GROUP:
                deleteGroup();
                break;
            case DELETE_STUDENT:
                deleteStudent();
                break;
            case DELETE_TEACHER:
                deleteTeacher();
                break;
            case DELETE_SUBJECT:
                deleteSubject();
                break;
            case DELETE_GTS:
                deleteGts();
                break;
            case DELETE_ADMIN:
                deleteAdmin();
                break;
            case DELETE_LESSON:
                deleteLesson();
                break;
            case DELETE_SKIP:
                deleteSkip();
                break;
            case DELETE_LAB:
                deleteLab();
                break;
            case DELETE_LS:
                deleteLs();
                break;
            case DELETE_INTERIM:
                deleteInterim();
                break;
        }
    }

    private void deleteGroup() {
        if (adminWnd.getGroup() != -1) {
            adminCtrl.deleteGroup(adminCtrl.getGroup(adminWnd.getGroup()));
            adminWnd.reloadGroup();
        } else
            showMessage("Не выбрана группа");
    }

    private void deleteStudent() {
        if (adminWnd.getGroup() != -1) {
            if (adminWnd.getStudent() != -1) {
                adminCtrl.deleteStudent(adminCtrl.getStudent(adminWnd.getGroup(), adminWnd.getStudent()));
                adminWnd.reloadStudent();
            } else
                showMessage("Не выбран студент");
        } else
            showMessage("Не выбрана группа");
    }

    private void deleteTeacher() {
        if (adminWnd.getTeacher() != -1) {
            adminCtrl.deleteTeacher(adminCtrl.getTeacher(adminWnd.getTeacher()));
            adminWnd.reloadTeacher();
        } else
            showMessage("Не выбран преподаватель");
    }

    private void deleteSubject() {
        if (adminWnd.getSubject() != -1) {
            adminCtrl.deleteSubject(adminCtrl.getSubject(adminWnd.getSubject()));
            adminWnd.reloadSubject();
        } else
            showMessage("Не выбран предмет");
    }

    private void deleteGts() {
        if (adminWnd.getGts() != -1) {
            adminCtrl.deleteGts(adminCtrl.getGts(adminWnd.getGts()));
            adminWnd.reloadGts();
        } else
            showMessage("Не выбрано назначение");
    }

    private void deleteAdmin() {
        if (adminWnd.getAdmin() != -1) {
            adminCtrl.deleteAdmin(adminCtrl.getAdmin(adminWnd.getAdmin()));
            adminWnd.reloadAdmin();
        } else
            showMessage("Не выбран администратор");
    }

    private void deleteLesson() {
        if (teacherWnd.getLesson() != -1) {
            teacherCtrl.deleteLesson(teacherCtrl.getLesson(teacherWnd.getSubject(), teacherWnd.getGroup(), teacherWnd.getLesson(), teacherWnd.getBegin(), teacherWnd.getEnd()));
            teacherWnd.reloadLesson();
        } else
            showMessage("Не выбрано занятие");
    }

    private void deleteSkip() {
        if (teacherWnd.getSkip() != -1) {
            teacherCtrl.deleteSkip(teacherCtrl.getSkip(teacherWnd.getSubject(), teacherWnd.getGroup(), teacherWnd.getLesson(), teacherWnd.getSkip(), teacherWnd.getBegin(), teacherWnd.getEnd()));
            teacherWnd.reloadSkip();
        } else
            showMessage("Не выбран отсутсвующий");
    }

    private void deleteLab() {
        if (teacherWnd.getLab() != -1) {
            teacherCtrl.deleteLab(teacherCtrl.getLab(teacherWnd.getSubject_l(), teacherWnd.getBegin_l(), teacherWnd.getEnd_l(), teacherWnd.getLab()));
            teacherWnd.reloadLab();
        } else
            showMessage("Не выбрано задание");
    }

    private void deleteLs() {
        if (teacherWnd.getLs() != -1) {
            teacherCtrl.deleteLs(teacherCtrl.getLs(teacherCtrl.getLab(teacherWnd.getSubject_l(), teacherWnd.getBegin_l(), teacherWnd.getEnd_l(), teacherWnd.getLab()), teacherCtrl.getGroup(teacherWnd.getSubject_l(), teacherWnd.getGroup_ls()), teacherWnd.getLs()));
            teacherWnd.reloadLs();
        } else
            showMessage("Не выбрана запись");
    }

    private void deleteInterim() {
        if (teacherWnd.getInterim() != -1) {
            teacherCtrl.deleteInterim(teacherCtrl.getInterim(teacherWnd.getSubject_l(), teacherWnd.getLab(), teacherWnd.getGroup_ls(), teacherWnd.getLs(), teacherWnd.getInterim(), teacherWnd.getBegin_l(), teacherWnd.getEnd_l()));
            teacherWnd.reloadInterim();
        } else
            showMessage("Не выбрана промежуточная сдача");
    }

    private void showMessage(String content) {
        JOptionPane.showMessageDialog(adminWnd, content, "Ошибка", JOptionPane.ERROR_MESSAGE);
    }

}
